package com.orkasgb.framework.springsecurity.sms;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class OrkasgbSMSAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = -8447558989998984221L;

    private final Object principal;

    private final Object credentials;

    public OrkasgbSMSAuthenticationToken(Object phoneNumber, Object code) {
        super(null);
        this.credentials = phoneNumber;
        this.principal = code;
        setAuthenticated(false);
    }

    public OrkasgbSMSAuthenticationToken(Object phoneNumber, Object userDetails, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.credentials = phoneNumber;
        this.principal = userDetails;
        super.setAuthenticated(true); // must use super, as authorities must be set
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted");
        }
        super.setAuthenticated(false);
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "[principal=" + principal + ", authenticated=" + isAuthenticated() + "]";
    }
}
